﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;

namespace DotHandler
{
    public abstract class Model : Node, ISerializable
    {
        #region Construtor
        public Model(){}
        #endregion

        #region Métodos
        protected void DeSerialize(SerializationInfo info, StreamingContext context)
        {
            var me = context.State;
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo prop in properties)
            {
                String name = prop.Name;
                String type = prop.PropertyType.ToString();
                switch (type)
                {
                    case "System.Int32": 
                        var value = (int)info.GetInt32(prop.Name);
                        prop.SetValue(
                          this,
                          value,
                          null
                         );
                        break;
                    case "System.Double" : prop.SetValue(
                           this,
                           info.GetDouble(prop.Name),
                           null
                          );
                        break;
                    case "System.String":
                        prop.SetValue(
                           this,
                           info.GetString(prop.Name),
                           null
                          );
                        break;
                    case "System.DateTime":
                        prop.SetValue(
                           this,
                           info.GetDateTime(prop.Name),
                           null
                          );
                        break;
                    case "System.Boolean":
                        prop.SetValue(
                           this,
                           info.GetBoolean(prop.Name),
                           null
                          );
                        break;
                    default :
                        prop.SetValue(
                           this,
                           info.GetString(prop.Name),
                           null
                          );
                        break;
                }
            }
        }

        protected Model(SerializationInfo info, StreamingContext context)
        {
            DeSerialize(info, context);
        }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo prop in properties)
            {
                string property = prop.Name;
                dynamic value = this.GetType().GetProperty(property).GetValue(this,null);
                info.AddValue(prop.Name, value);
            }
        }

        #endregion
    }
}
